﻿using System;
using AudioStudio;
using Gameplay.PVE.Survivor;
using Pathfinding;
using UnityEngine;
using Yoozoo.Gameplay.Liberty;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Mars.Got;

namespace Yoozoo.Gameplay.City
{
    public class AirWallDefiner : MonoBehaviour
    {
        [Header("碰撞器")]
        public Collider WallCollider;
        
        [Header("警告特效，碰撞时显示")]
        public Transform WarningEffect;

        [Header("封锁线特效，常驻显示")]
        public Transform LockLineEffect;

        [Header("空气墙解锁特效")]
        public Transform UnlockEffect;

        private AirWallCollision _wallCollision;

        public bool isSurvivor;
        
        private void OnEnable()
        {
            if (WarningEffect)
            {
                WarningEffect.gameObject.SetActive(false);
            }

            if (LockLineEffect)
            {
                LockLineEffect.gameObject.SetActive(true);
            }

            if (UnlockEffect)
            {
                UnlockEffect.gameObject.SetActive(false);
            }

            if (WallCollider)
            {
                _wallCollision = WallCollider.transform.GetOrAddCompoment<AirWallCollision>();
                _wallCollision.onCollisonEnter += OnCollisionEnter;
                _wallCollision.onCollisionExit += OnCollisionExit;
                
                NavmeshCut navmeshCut = WallCollider.GetComponent<NavmeshCut>();
                if (navmeshCut)
                {
                    navmeshCut.useRotationAndScale = true;
                    Vector3 scale = WallCollider.transform.lossyScale;
                    // navmeshCut.rectangleSize = new Vector2(scale.x, scale.z * 4);
                    navmeshCut.height = scale.y * 2;
                }
            }
            
        }

        public void PlayUnlockEffect()
        {
            if (WarningEffect) WarningEffect.gameObject.SetActive(false);
            if (LockLineEffect) LockLineEffect.gameObject.SetActive(false);
            if (UnlockEffect) UnlockEffect.gameObject.SetActive(false);
            if (UnlockEffect) UnlockEffect.gameObject.SetActive(true);
            
            
        }

        private bool CheckIsTrigger(Collider collider)
        {
            var isTrigger = false;
            if (isSurvivor)
            {
                isTrigger = BattleHelper.IsSurvivorTeamCollider(collider);
            }
            else
            {
                isTrigger = LibertyAIUtils.IsPlayerTriggerCollider(collider);
            }
            return isTrigger;
        }
        
        private void OnCollisionEnter(Collision collision)
        {
            if (CheckIsTrigger(collision.collider))
            {
                Vector3 playerPos = collision.collider.transform.position;
                Vector3 localPos = transform.InverseTransformPoint(playerPos);

                Vector3 weLocalPos = WarningEffect.localPosition;
                WarningEffect.localPosition = new Vector3(localPos.x, 2.04f, weLocalPos.z);
                
                WarningEffect.gameObject.SetActive(false);
                WarningEffect.gameObject.SetActive(true);
                AudioManager.PlaySound("am_street_edgetip");
                Invoke("StopSound",1);
                if (isSurvivor)
                {
                    StageManager.Instance.TeamGroup.SetIsInAirWall(true);
                }
            }
        }

        private void OnCollisionExit(Collision collision)
        {
            if (CheckIsTrigger(collision.collider))
            {
                StopSound();
                if (isSurvivor)
                {
                    StageManager.Instance.TeamGroup.SetIsInAirWall(false);
                }
            } 
        }

        private void StopSound()
        {
            AudioManager.StopSound("am_street_edgetip");
        }

        private void OnDisable()
        {
            if (_wallCollision)
            {
                _wallCollision.onCollisonEnter -= OnCollisionEnter;
                _wallCollision.onCollisionExit -= OnCollisionExit;
            }
        }
    }
}